package com.android.thelastnoet;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.thelastnoet.bean.User;
import com.android.thelastnoet.db.UserMapper;

public class LoginActivity extends AppCompatActivity {
    SharedPreferences data;
    SharedPreferences.Editor editor;
    EditText userName, pwd, ver_pwd;
    UserMapper userMapper= new UserMapper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        data = getSharedPreferences("data", MODE_PRIVATE);
        editor = getSharedPreferences("data", MODE_PRIVATE).edit();
        userName = findViewById(R.id.userName);
        pwd = findViewById(R.id.pwd);
        ver_pwd = findViewById(R.id.ver_pwd);

        pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        ver_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

    }


    @SuppressLint("CommitPrefEdits")
    public void onClock(View view) {
        switch (view.getId()) {
            case R.id.btn_to_register:
                userName.setFocusable(true);
                findViewById(R.id.btn_login).setVisibility(View.GONE);
                findViewById(R.id.btn_to_register).setVisibility(View.GONE);
                findViewById(R.id.btn_register).setVisibility(View.VISIBLE);
                findViewById(R.id.verify_login_pwd).setVisibility(View.VISIBLE);
                findViewById(R.id.bottom_bar).setVisibility(View.GONE);
                TextView login_register_tv = (TextView) findViewById(R.id.login_register_tv);
                login_register_tv.setText(R.string.register);
                break;
            case R.id.btn_login:
/*
                Log.d("Login", userName.getText() + " " + pwd.getText() + " " + ver_pwd.getText());
*/         if (String.valueOf(userName.getText()).isEmpty()&& String.valueOf(pwd.getText()).isEmpty()) {
                    Toast.makeText(this, "请输入账号、密码登陆！", Toast.LENGTH_SHORT).show();
                } else if (String.valueOf(userName.getText()).isEmpty()) {
                    Toast.makeText(this, "账号不得为空！", Toast.LENGTH_SHORT).show();
                } else if (String.valueOf(pwd.getText()).isEmpty()) {
                    Toast.makeText(this, "密码不得为空！", Toast.LENGTH_SHORT).show();
                }else {
                    User userInfo = userMapper.getUserInfo(String.valueOf(userName.getText()));
                    String userName = userInfo.getUserName();
                    String password = userInfo.getPassword();
                    String id = String.valueOf(userInfo.getId());
/*                    if (!String.valueOf(data.getString("userName", "")).equals
                            (String.valueOf(this.userName.getText()))) {
                        Toast.makeText(this, "未查询到该账号记录,请注册", Toast.LENGTH_SHORT).show();
                        this.userName.setText("");
                        pwd.setText("");
                        toRegister();
                    }
                    else */             if (userName.equals(String.valueOf(this.userName.getText()))
                            && password.equals(String.valueOf(pwd.getText()))) {
                    editor.putString("isLogin", "true");
                    editor.putString("id",id);
                    editor.commit();
                    editor.apply();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    }else {
                        Toast.makeText(this, "账号或密码错误!", Toast.LENGTH_SHORT).show();
                        this.userName.setText("");
                        pwd.setText("");
                    }
/*                    if (!String.valueOf(data.getString("userName", "")).equals
                            (String.valueOf(this.userName.getText()))) {
                        Toast.makeText(this, "未查询到该账号记录,请注册", Toast.LENGTH_SHORT).show();
                        this.userName.setText("");
                        pwd.setText("");
                        toRegister();
                    }
                    else if (String.valueOf(data.getString("userName", "")).equals
                            (String.valueOf(this.userName.getText()))
                            && String.valueOf(data.getString("pwd", "")).equals
                            (String.valueOf(pwd.getText()))) {
                        editor.putString("isLogin", "true");
                        editor.apply();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    else {
                        Toast.makeText(this, "账号或密码错误!", Toast.LENGTH_SHORT).show();
                        this.userName.setText("");
                        pwd.setText("");
                    }*/
                }
                break;
            case R.id.btn_register:
                if (String.valueOf(userName.getText()).isEmpty()&& String.valueOf(pwd.getText()).isEmpty()&& String.valueOf(ver_pwd.getText()).isEmpty()) {
                    Toast.makeText(this, "请输入注册信息！", Toast.LENGTH_SHORT).show();
                } else if (String.valueOf(userName.getText()).isEmpty()) {
                    Toast.makeText(this, "账号不得为空！", Toast.LENGTH_SHORT).show();
                } else if (String.valueOf(pwd.getText()).isEmpty()) {
                    Toast.makeText(this, "密码不得为空！", Toast.LENGTH_SHORT).show();
                } else if (String.valueOf(ver_pwd.getText()).isEmpty()) {
                    Toast.makeText(this, "确定密码不得为空！", Toast.LENGTH_SHORT).show();
                } else {
                    if (String.valueOf(pwd.getText()).equals(String.valueOf(ver_pwd.getText()))) {
                        editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                        editor.putString("userName", String.valueOf(userName.getText()));
                        editor.putString("pwd", String.valueOf(pwd.getText()));
                        editor.apply();
                        User user=new User();
                        user.setUserName(String.valueOf(userName.getText()));
                        user.setPassword(String.valueOf(pwd.getText()));
                        userMapper.addUser(user);
                        Toast.makeText(this, "注册成功！", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginActivity.this, LoginActivity.class);
                        startActivity(intent);
                    } else if (!String.valueOf(pwd.getText()).equals(String.valueOf(ver_pwd.getText()))) {
                        Toast.makeText(this, "两次输入的密码不相同！", Toast.LENGTH_SHORT).show();
                        pwd.setText("");
                        ver_pwd.setText("");
                    }
                }
        }
    }

    private void toRegister() {
        userName.setFocusable(true);
        findViewById(R.id.btn_login).setVisibility(View.GONE);
        findViewById(R.id.btn_to_register).setVisibility(View.GONE);
        findViewById(R.id.btn_register).setVisibility(View.VISIBLE);
        findViewById(R.id.verify_login_pwd).setVisibility(View.VISIBLE);
        findViewById(R.id.bottom_bar).setVisibility(View.GONE);
        TextView login_register_tv = (TextView) findViewById(R.id.login_register_tv);
        login_register_tv.setText("注册");
    }
}

/*
* 主界面中根据该用户的id查询出其所有便签，除了私密便签和被放入废纸篓中的便签，点击头部TextView“便签”选择不同文件夹，点击屏幕右下方的浮动按钮添加新便签*/